# -*- coding:UTF-8 -*-
from datetime import timedelta
from flask import Flask
from flask_cors import CORS
from web_app.record_view import record
from web_app.record_day_view import record_day
from web_app.index import indexx
from flask_jwt import JWT
from werkzeug.security import safe_str_cmp

from db import ususer_db


app = Flask(__name__)


app.register_blueprint(record, url_prefix='/api/record')
app.register_blueprint(record_day, url_prefix='/api/record')
app.register_blueprint(indexx, url_prefix='/api/index')


def authenticate(username, password):
    user = ususer_db.find_one({'username': username, 'password': password})
    print({'username': username, 'password': password}, user)
    if user and safe_str_cmp(password.encode('utf-8'), user['password'].encode('utf-8')):
        return user

def identity(payload):
    user_id = payload['identity']
    return ususer_db.find_one({'id': user_id})

app.config['SECRET_KEY'] = 'kkkkkBBBBoooo'
app.config['JWT_AUTH_URL_RULE'] = '/api/auth'
app.config['JWT_EXPIRATION_DELTA'] = timedelta(seconds=10 * 3600)

jwt = JWT(app, authenticate, identity)


if __name__ == '__main__':

    app.debug = False # 设置调试模式，生产模式的时候要关掉debug
    # app.config['JSON_AS_ASCII'] = False
    CORS(app, supports_credentials=True)
    print( app.url_map )
    app.run(host="0.0.0.0", port=7890)
    # search = "江苏板块"
    # detail = 0
    # seach(search, detail)
    
    
    